package com.bootx.controller.api.admin;

import com.bootx.common.Pageable;
import com.bootx.common.Result;
import com.bootx.controller.BaseController;
import com.bootx.entity.BaseEntity;
import com.bootx.entity.WriteTool;
import com.bootx.pojo.FormItem;
import com.bootx.service.WriteToolCategoryService;
import com.bootx.service.WriteToolService;
import com.bootx.util.JsonUtils;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author black
 */
@RestController("apiAdminWriteToolController")
@RequestMapping("/api/admin/writeTool")
@CrossOrigin
public class WriteToolController extends BaseController {

    @Resource
    private WriteToolService writeToolService;

    @Resource
    private WriteToolCategoryService writeToolCategoryService;

    @PostMapping("/list")
    @JsonView(BaseEntity.PageView.class)
    public Result list(Pageable pageable, String name, Long textAppCategoryId){
        pageable.setPageSize(1000);
        return Result.success(writeToolService.findPage(pageable,name,writeToolCategoryService.find(textAppCategoryId)));
    }

    @PostMapping("/formItem")
    public Result formItem(Long appId){
        WriteTool textApp = writeToolService.find(appId);
        if(textApp==null){
            return Result.success(Collections.EMPTY_LIST);
        }
        try {
            return Result.success(textApp.getFormItems().stream().map(item->{
                Map<String,Object> map = new HashMap<>();
                map.put("defaultValue",item.getDefaultValue());
                map.put("key",item.getLabel());
                map.put("label",item.getLabel());
                map.put("options", StringUtils.join(item.getOptions(),","));
                map.put("type",item.getType());
                map.put("isRequired",item.getIsRequired());
                map.put("placeholder",item.getPlaceholder());
                map.put("min",item.getMin());
                map.put("max",item.getMax());
                map.put("maxLines",item.getMaxLines());
                map.put("minLines",item.getMinLines());
                return map;
            }));
        }catch (Exception e){
            e.printStackTrace();
            textApp.setFormItems(new ArrayList<>());
            writeToolService.update(textApp);
            return Result.success(Collections.EMPTY_LIST);
        }
    }
    @PostMapping("/formItemSave")
    public Result formItemSave(Long appId,String formItems){
        WriteTool textApp = writeToolService.find(appId);
        if(textApp==null){
            return Result.success(Collections.EMPTY_LIST);
        }
        textApp.setFormItems(JsonUtils.toObject(formItems, new TypeReference<List<FormItem>>() {
        }));
        writeToolService.update(textApp);
        try {
            return Result.success(textApp.getFormItems());
        }catch (Exception e){
            e.printStackTrace();
            return Result.success(Collections.EMPTY_LIST);
        }
    }

    @PostMapping("/save")
    public Result save(WriteTool writeTool,Long textAppCategoryId,String formListStr){
        writeTool.setFormItems(JsonUtils.toObject(formListStr, new TypeReference<List<FormItem>>() {
        }));
        writeToolService.save(writeTool);
        return Result.success();
    }
    @PostMapping("/detail")
    @JsonView(BaseEntity.ViewView.class)
    public Result detail(Long id){
        return Result.success(writeToolService.find(id));
    }

    @PostMapping("/update")
    public Result update(WriteTool writeTool,Long textAppCategoryId,String formListStr){
        writeTool.setFormItems(JsonUtils.toObject(formListStr, new TypeReference<List<FormItem>>() {
        }));
        writeToolService.update(writeTool);
        return Result.success();
    }
    @PostMapping("/delete")
    public Result delete(Long[] ids){
        writeToolService.delete(ids);
        return Result.success();
    }

    @PostMapping("/category")
    public Result category(){
        return Result.success(jdbcTemplate.queryForList("select id,name from write_tool_category;"));
    }

}
